/*
 * GenericGraphicsObjectEditor.java
 *
 * Created on 19. huhtikuuta 2007, 23:04
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package flowchart.editors;

import flowchart.nodes.*;
import java.awt.*;

/**
 *
 * @author jl
 */
public class CircleGraphicsObjectEditor implements GraphicsObjectEditor {

    protected GraphicsObject obj;

    /** Creates a new instance of GenericGraphicsObjectEditor */
    public CircleGraphicsObjectEditor() {
        obj = null;
    }

    public GraphicsObject getGraphicsObject() {
        return obj;
    }

    public void setGraphicsObject(GraphicsObject obj) {
        this.obj = obj;
    }

    public HitTestInfo hitTest(int x, int y) {

        Rectangle bounds = obj.getBounds();
        int right = bounds.x + bounds.width;
        int bottom = bounds.y + bounds.height;
        if (bounds.contains(x, y)) {
            // if right edge
            if (right - x < 5) {
                // if bottom-right
                if (bottom - y < 5) {
                    return new HitTestInfo(8, x - right, y - bottom);
                }
                // if top-right
                if (y - bounds.y < 5) {
                    return new HitTestInfo(3, x - right, y - bounds.y);
                }
            }
            // if left edge
            if (x - bounds.x < 5) {
                // if bottom-left
                if (bottom - y < 5) {
                    return new HitTestInfo(6, x - bounds.x, y - bottom);
                }
                // if top-left
                if (y - bounds.y < 5) {
                    return new HitTestInfo(1, x - bounds.x, y - bounds.y);
                }
            }

            // if elsewhere
            return new HitTestInfo(HitTestInfo.NOHANDLE, x - bounds.x, y - bounds.y);
        }
        // no hit this time
        return new HitTestInfo();

    }

    public boolean moveHandle(HitTestInfo hInfo, int nx, int ny) {

        Rectangle bounds = (Rectangle) obj.getBounds().clone();
        int right = bounds.x + bounds.width;
        int bottom = bounds.y + bounds.height;

        if (hInfo.code == HitTestInfo.NOHANDLE) {
            // move complete object
            bounds.setLocation(nx - hInfo.offsx, ny - hInfo.offsy);



        } else if (hInfo.code == 8) {
            // move bottom right corner
            //
            bounds.setSize(
                    Math.max(5, nx - bounds.x - hInfo.offsx),
                    Math.max(5, nx - bounds.x - hInfo.offsx));
        } else if (hInfo.code == 3) {
            // move top right corner
            bounds.setLocation(bounds.x, ny - hInfo.offsy);
            bounds.setSize(
                    Math.max(5, nx - bounds.x - hInfo.offsx),
                    Math.max(5, nx - bounds.x - hInfo.offsy));
        } else if (hInfo.code == 6) {
            // move bottom left corner
            bounds.setLocation(nx - hInfo.offsx, bounds.y);
            bounds.setSize(
                    Math.max(5, right - nx + hInfo.offsx),
                    Math.max(5, right - nx + hInfo.offsx));
        } else if (hInfo.code == 1) {
            // move top left corner
            bounds.setLocation(nx - hInfo.offsx, ny - hInfo.offsy);
            bounds.setSize(
                    Math.max(5, right - nx + hInfo.offsx),
                    Math.max(5, right - nx + hInfo.offsx));
        }


        obj.setBounds(bounds);
        return true;
    }

    public void drawHandles(Graphics2D g, Color fill, Color border) {
        Rectangle bounds = obj.getBounds();
        Rectangle r = new Rectangle();
        Color old = g.getColor();
        // top-left
        r.setBounds(bounds.x, bounds.y, 5, 5);
        g.setColor(fill);
        g.fill(r);
        g.setColor(border);
        g.draw(r);
        // top-right
        r.setLocation(bounds.x + bounds.width - 5, bounds.y);
        g.setColor(fill);
        g.fill(r);
        g.setColor(border);
        g.draw(r);
        // bottom-left
        r.setLocation(bounds.x, bounds.y + bounds.height - 5);
        g.setColor(fill);
        g.fill(r);
        g.setColor(border);
        g.draw(r);
        // bottom right
        r.setLocation(bounds.x + bounds.width - 5, bounds.y + bounds.height - 5);
        g.setColor(fill);
        g.fill(r);
        g.setColor(border);
        g.draw(r);
        g.setColor(old);
    }
}
